﻿using System.Collections;
using System.Collections.Generic;
using System;

namespace Core
{
    public class Move
    {

        private int _score;                               //分数
        private int[,] _map;                             //格子map  二维
        private Random _radom;                   // 随机种子
        private int _gird=4;                                 //格子
        private int _beforescore;

        public Move()
        {
            _map = new int[_gird, _gird];
            _radom = new Random();
        }

        public int[,] Map
        {
            get { return _map; }
        }

        /// <summary>
        /// 获取当前分数
        /// </summary>
        /// <returns></returns>
        public int Score()
        {
            return _score;
        }

        /// <summary>
        /// 历史最高分数
        /// </summary>
        /// <returns></returns>
        public int BeforeScore()
        {
            if(_score>_beforescore)
            {
                _beforescore = _score;
            }
            return _beforescore;
        }

        /// <summary>
        ///  滑动
        /// </summary>
        /// <param name="dir">滑动方向</param>
        public void Slide(Direction dir)
        {
            switch (dir)
            {
                case Direction.Up:
                    up();
                    break;
                case Direction.Down:
                    down();
                    break;
                case Direction.Left:
                    left();
                    break;
                case Direction.Right:
                    right();
                    break;
            }
        }


        //左
        private void left()
        {
            for (int i=0; i < _gird; i++)
            {
                int j = 0;

                //移动
                for (j = 0; j < _gird; j++)
                {
                    if (_map[i, j] == 0 && _map[i, j + 1] != 0)
                    {
                        _map[i, j] = _map[i, j + 1];
                        _map[i, j + 1] = 0;
                    }
                }

                //合并
                for (j = 0; j < _gird; j++) 
                {
                    if (_map[i, j] != 0 && _map[i, j] == _map[i, j + 1])
                    { 
                        _map[i, j] = _map[i, j] * 2;
                        if((int)_map[i,j]>_score)
                        {
                            _score = (int)_map[i,j]; 
                        }
                        _map[i, j + 1] = 0; 
                    }
                }

                //移动
                for (j = 0; j < _gird; j++)
                {
                    if (_map[i, j] == 0 && _map[i, j + 1] != 0)
                    {
                        _map[i, j] = _map[i, j + 1];
                        _map[i, j + 1] = 0;
                    }
                }
                


            }
        }


        //右
        private void right()
        {
            for(int i=0;i<_gird;i++)
            {
                int j = 3;
                //移动
                for(j=4;0<=j&&j<_gird;j--)
                {
                    if(_map[i,j]==0&&_map[i,j-1]!=0)
                    {
                        _map[i, j] = _map[i, j - 1];
                        _map[i, j - 1] = 0;
                    }

                }
                //合并
                for(j=4; 0<=j&&j<_gird;j--)
                {
                    if(_map[i,j]==_map[i,j-1]&&_map[i,j]!=0)
                    {
                        _map[i,j] = _map[i, j] * 2;
                        if((int)_map[i,j]>_score)
                        {
                            _score = _map[i, j];
                        }
                        _map[i, j - 1] = 0;
                    }
                }
                //再次移动
                for(j=4;0<=j&&j<_gird;j--)
                {
                    if(_map[i,j]==0&&_map[i,j-1]!=0)
                    {
                        _map[i, j] = _map[i, j - 1];
                        _map[i, j - 1] = 0;
                    }
                }

            }
        }

        //上
        private void up()
        {
            for (int j = 0; j < _gird; j++)
            {
                int i;
                for (i = 0; i < _gird; i++)
                {
                    if (_map[j, i] == 0 && _map[j, i + 1] != 0)
                    {
                        _map[j, i] = _map[j, i + 1];
                        _map[j, i + 1] = 0;
                    }
                }
                //合并
                for (i = 0; i < _gird; i++)
                {
                    if (_map[j, i] == _map[j, i + 1] && _map[j, i] != 0)
                    {
                        _map[j, i] = _map[j, i] * 2;
                        if ((int)_map[j, i] > _score)
                        {
                            _score = _map[j, i];
                        }
                        _map[j, i + 1] = 0;
                    }
                }
                //再移动
                for (i = 0; i < _gird; i++)
                {

                    if (_map[j, i] == 0 && _map[j, i + 1] != 0)
                    {
                        _map[j, i] = _map[j, i + 1];
                        _map[j, i + 1] = 0;
                    }
                }
            }
        }

        //下
        private void down()
        {

            for(int j=0;j<_gird;j++)
            {
                int i;
                for(i=3;0<=i&&i<_gird;i--)
                    if(_map[j,i]==0&&_map[j,i-1]!=0)
                    {
                        _map[j, i] = _map[j, i - 1];
                        _map[j, i - 1] = 0;
                    }
                //合并
                for (i = 3; 0 <= i &&i< _gird; i--)
                {
                    if(_map[j,i]==_map[j,i-1]&&_map[j,i]!=0)
                    {
                        _map[j, i] = _map[j, i] * 2;
                        if((int)_map[j,i]>_score)
                        {
                            _score = _map[j, i];
                        }
                        _map[j, i - 1] = 0;
                    }
                }
                //再移动
                for(i=3;0<=i&&i<_gird;i--)
                {
                    if(_map[j,i]==0&&_map[j,i-1]!=0)
                    {
                        _map[j, i] = -Map[j, i - 1];
                        _map[j, i - 1] = 0;
                    }
                }
            }
        }


       


        //返回随机2 或 4
        private int random()
        {
            int resu =  _radom.Next(0, 100);
            if (resu >80 ) return 4;
            return 2;
        }






    }
}


